package ink.lovejinhu.swardToOffer;

/**
 * @Author: jin
 * @Date: 2021/9/15 11:19
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Offer_001_II {
    /**
     * 整数除法
     * 求a/b的商
     *
     * @param a
     * @param b
     * @return
     */
    public int divide(int a, int b) {
        long aL = (long) a;
        long bL = (long) b;
        boolean flag = true;
        if (a == 0) {
            return 0;
        }
        if (aL > 0 && bL > 0) {

        } else if (aL > 0 && bL < 0) {
            flag = false;
            bL = -bL;
        } else if (aL < 0 && bL > 0) {
            flag = false;
            aL = -aL;
        } else {
            aL = -aL;
            bL = -bL;
        }
        System.out.println(aL);
        System.out.println(bL);
        System.out.println(flag);
        /**
         * 先判断符号是否相同
         */
        long sum = 0;
        for (long i = 1; ; i++) {
            sum += bL;
            if (sum > aL) {
                if (flag) {
                    return (int) (i - 1);
                } else {
                    return (int) (1 - i);
                }
            } else if (sum == aL) {
                if (flag) {
                    return (int) i;
                } else {
                    return (int) -i;
                }

            } else {
                continue;
            }
        }
    }

    public static void main(String[] args) {
        /**
         * -2147483648
         * -1
         */
        int a = -2147483648;
        int b = -1;
        Offer_001_II offer_001_ii = new Offer_001_II();
        int divide = offer_001_ii.divide(a, b);
        System.out.println(divide);
    }
}
